﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ROOT.CIMV2.Win32;
using System.Threading;
using System.Diagnostics;

namespace ProcessInterceptor
{
	public partial class MainForm : Form
	{

		public MainForm()
		{
			InitializeComponent();
			RefreshList();			
			this.Text = "Process interceptor";
		}

		bool die = false;

		ProcessWatcher procWatcher;
		Thread watchThread;
		List<string> ProcessesToKill = new List<string>();

		private void RefreshList()
		{
			try
			{
				StreamReader reader = new StreamReader("process.list");

				while (!reader.EndOfStream)
					ProcessList.Items.Add(reader.ReadLine());
				reader.Close();
				foreach (string item in ProcessList.Items)
					ProcessesToKill.Add(item);
			}
			catch { }
		}

		private void WriteList()
		{
			StreamWriter writer = new StreamWriter("process.list", false);
			List<string> procs = new List<string>();
			
			foreach (string item in ProcessList.Items)
				writer.WriteLine(item);
			writer.Close();
		}

		private List<Thread> threads = new List<Thread>();

		private void StartBtn_Click(object sender, EventArgs e)
		{
			PassForm form = new PassForm();
			form.ShowDialog();

			StopBtn.Enabled = true;
			StartBtn.Enabled = ProcessList.Enabled = groupBox1.Enabled = AddBtn.Enabled =
				DeleteBtn.Enabled = RefreshBtn.Enabled = newProcBox.Enabled = false;

			Process[] processes = Process.GetProcesses();
			foreach (Process proc_ in processes)
				foreach (string process in ProcessesToKill)
				{
					if (proc_.ProcessName + ".exe" == process)
						proc_.Kill();
				}
/*
			foreach (string process in ProcessList.Items)
			{
				ProcessWatcher procWatcher = new ProcessWatcher(process);
				procWatcher.ProcessCreated += new ProcessEventHandler(procWatcher_ProcessCreated);
				procWatcher.Start();

				Thread watchThread = new Thread(new ParameterizedThreadStart(procWatchThreadProc));
				threads.Add(watchThread);
				watchThread.Start(procWatcher);
			}
 */
			procWatcher = new ProcessWatcher(null);
			procWatcher.ProcessCreated += new ProcessEventHandler(procWatcher_ProcessCreated);
			procWatcher.Start();

			watchThread = new Thread(procWatchThreadProc);
			watchThread.Start();			
		}

		private void procWatchThreadProc()
		{
			while (!die)
			{
				try
				{
					procWatcher.WaitForNextEvent(); //ожидаем следующее событие
				}
				catch
				{
				}
			}
		}

		void procWatcher_ProcessCreated(Win32_Process process)
		{
			if (this.InvokeRequired)
			{
				this.Invoke(new ProcessEventHandler(procWatcher_ProcessCreated), new object [] { process });
				return;
			}
		
			Process proc = Process.GetProcessById((int)process.ProcessId);
			foreach (string dieProcess in ProcessList.Items)
				if (proc.ProcessName + ".exe" == dieProcess)
					proc.Kill();
		}

		private void DeleteBtn_Click(object sender, EventArgs e)
		{
			ProcessList.Items.Remove(ProcessList.SelectedItem);
			WriteList();
		}

		private void AddBtn_Click(object sender, EventArgs e)
		{
			if (newProcBox.Text == "")
				return; 

			ProcessList.Items.Add(newProcBox.Text);
			WriteList();
		}

		private void StopBtn_Click(object sender, EventArgs e)
		{
			AskForm form = new AskForm();
			DialogResult dr = form.ShowDialog();
			if (dr == DialogResult.Cancel)
				return;
		
			StopBtn.Enabled = false;
			
			StartBtn.Enabled = ProcessList.Enabled = groupBox1.Enabled = AddBtn.Enabled =
				DeleteBtn.Enabled = RefreshBtn.Enabled = newProcBox.Enabled = true;

			die = true;
			watchThread.Join();
			procWatcher.Stop();
		}
		
		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
		    if (!StartBtn.Enabled)
		    {
		        AskForm askForm = new AskForm();
				if (askForm.ShowDialog() != DialogResult.OK)
					e.Cancel = true;
		     }							
		}
	}
}
